<?php
/**
 * Created by PhpStorm.
 * User: sen
 * Date: 16/7/13
 * Time: 下午4:18
 */
namespace app\controllers;
use yii\web\Controller;
use app\models\Commodity;
use app\models\Category;
use app\models\User;



class AuctionlistController extends Controller{

    public function init(){
        $this->enableCsrfValidation = false;
        if(!\Yii::$app->user->id){
            $url = \Yii::$app->request->getPathInfo();
            return $this->redirect('../login/index?url='.$url);
        }
        $userId = \Yii::$app->user->id;
        $user = new User();
        $admin = $user->findIdentity($userId);
        if($admin['type']==1 or $admin['type']==0){
            return $this->redirect('../login/logout');
        }
    }

    //加载view（默认加载出售中的宝贝）
    public function actionIndex(){
        $view = \Yii::$app->view;
        $view->params['menuitemid']=22;
        return $this->render('index');
    }

    public function actionWarehouse(){
        $view = \Yii::$app->view;
        $view->params['menuitemid']=23;
        return $this->render('warehouse');

    }

    public function actionCategory(){
        $view = \Yii::$app->view;
        $view->params['menuitemid']=24;
        return $this->render('category');

    }

    //获取出售中的商品列表数据
    public function actionSaleTableData(){

        //获取相应字段
        $request = \Yii::$app->request;
        $offset = $request->get('offset');
        $limit = $request->get('limit');
        $search = $request->get('search');
        $is_sale = 1;

        $model = new Commodity();
        $rs = $model->getTableData($limit,$offset,$search,$is_sale);
        echo json_encode($rs);

    }

    //仓库出售中的列表
    public function actionWarehouseTableData(){

        //获取相应字段
        $request = \Yii::$app->request;
        $offset = $request->get('offset');
        $limit = $request->get('limit');
        $search = $request->get('search');
        $is_sale = 0;

        $model = new Commodity();
        $rs = $model->getTableData($limit,$offset,$search,$is_sale);
        echo json_encode($rs);

    }

    //商品下架操作
    public function actionOnWarehouse(){
        //获取category_id字段
        $request = \Yii::$app->request;
        $commodity_id = $request->get('commodity_id');
        //下架标志位
        echo Commodity::updateAll(['is_sale'=>'0'],['commodity_id'=>$commodity_id]);

    }

    //商品上架操作
    public function actionOnSale(){
        //获取category_id字段
        $request = \Yii::$app->request;
        $commodity_id = $request->get('commodity_id');
        //下架标志位
        echo Commodity::updateAll(['is_sale'=>'1'],['commodity_id'=>$commodity_id]);

    }

    //商品删除操作
    public function actionRemoveCommodity(){
        //获取attr_opt_id字段
        $request = \Yii::$app->request;
        $commodity_id = $request->get('commodity_id');
        //删除标志位
        echo Commodity::updateAll(['del_flag'=>'0'],['commodity_id'=>$commodity_id]);
    }

    //商品设置推荐
    public function actionSetRecommended(){
        //获取attr_opt_id字段
        $request = \Yii::$app->request;
        $commodity_id = $request->get('commodity_id');
        //删除标志位
        echo Commodity::updateAll(['is_hot'=>'1'],['commodity_id'=>$commodity_id]);
    }

    //商品取消推荐
    public function actionCancelRecommended(){
        //获取attr_opt_id字段
        $request = \Yii::$app->request;
        $commodity_id = $request->get('commodity_id');
        //删除标志位
        echo Commodity::updateAll(['is_hot'=>'0'],['commodity_id'=>$commodity_id]);
    }

    //分类的列表
    public function actionCategoryTableData(){

        //获取相应字段
        $request = \Yii::$app->request;
        $offset = $request->get('offset');
        $limit = $request->get('limit');
        $search = $request->get('search');

        $model = new Category();
        $rs = $model->getTableData($limit,$offset,$search);
        echo json_encode($rs);

    }

    //分类删除操作
    public function actionRemoveCategory(){
        //获取category_id字段
        $request = \Yii::$app->request;
        $category_id = $request->get('category_id');
        //删除标志位
        echo Category::updateAll(['del_flag'=>'0'],['category_id'=>$category_id]);
    }

    //获取一级分类目录
    public function actionFirstCategoryList(){
        $categoryModel = new Category();
        $rs = $categoryModel->getFirstCategoryList();
        echo json_encode($rs);
    }


    //添加分类操作
    public function actionAddSecondCategory(){

        $request = \Yii::$app->request;
        $pid = $request->post('pid');
        $name = $request->post('category_name');

        $model = new Category();
        $model->pid=$pid;
        $model->category_name=$name;
        $model->create_time=date('Y-m-d h:i:s',time());

        if($model->save() > 0){
            echo json_encode('1');
        } else{
            echo json_encode('0');
        }
    }


}